package gui.buttons 
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import gui.PositionInfo;
	import utils.Logger;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class RadioButton extends LabelButton
	{
		private var _icon:BaseButton;
		
		public static const UP_ICON:String = "UP_ICON";
		public static const OVER_ICON:String = "OVER_ICON";
		public static const DOWN_ICON:String = "DOWN_ICON";
		
		public static const SELECTED_UP_ICON: String = "SELECTED_UP_ICON";
		public static const SELECTED_OVER_ICON:String = "SELECTED_OVER_ICON";
		public static const SELECTED_DOWN_ICON:String = "SELECTED_DOWN_ICON";
		
		public static const UP_DELIMITER:String = "UP_DELIMITER";
		public static const SELECTED_DELIMITER:String = "SELECTED_DELIMITER";
		
		private var radius:Number = 7;
		private var testShape:Shape;
		private var _textPadding:Number = 4;
		protected var _groupName:String = "defaultRadioGroup";
		protected static var buttons:Vector.<RadioButton>;
		
		protected var _iconPosition:PositionInfo;
		
		protected var _value:Object;
		
		private var _delimiterPos:PositionInfo;
		private var _currentDelimiter:DisplayObject = null;
		
		public function RadioButton(parent:DisplayObjectContainer = null, 
										xpos:Number = 0, ypos:Number =  0) 
		{
			addButton(this);
			super(parent, xpos, ypos);
			
		}
		
		override protected function onComponentInit():void 
		{
			super.onComponentInit();
			
			//setupIconDefaulStyle();
			_delimiterPos = new PositionInfo();
			_delimiterPos.align = PositionInfo.ALIGN_BOTTOM;
			_delimiterPos.addAlign(PositionInfo.ALIGN_RIGHT);
			_delimiterPos.bottomPadding = -2;
			
			_iconPosition = new PositionInfo();
			
			textPosition.posX = width + _textPadding;
			textPosition.posY = 0.5;
			textPosition.addAlign(PositionInfo.ALIGN_CENTR_Y);
			_toggle = true;
			//height = _button.upState.height;
			/*testShape = new Shape();
			addChild(testShape);
			var gr:Graphics = testShape.graphics;
			gr.lineStyle(1,0xCCCCCC);
			//gr.beginFill(0xCCCCCC);
			gr.drawRect(0, 0, 50, 10);
			gr.endFill();*/
			this.addEventListener(MouseEvent.CLICK, onButtonClick);
		}
		
		override protected function setTextPosition():void 
		{
			super.setTextPosition();
		}
		
		private function onButtonClick(e:MouseEvent):void 
		{
			selected = true;
		}
		
		protected static function clear(rb:RadioButton):void
		{
			for(var i:uint = 0; i < buttons.length; i++)
			{
				if(buttons[i] != rb && buttons[i].groupName == rb.groupName)
				{
					buttons[i]._selected = false;
					buttons[i].validateNow();
				}
			}
		}
		
		protected static function addButton(rb:RadioButton):void
		{
			if(buttons == null)
			{
				buttons = new Vector.<RadioButton>();
			}
			buttons.push(rb);
		}
		
		
		
		
		private function fuseStyles(skinStyle:String, iconStyle:String):DisplayObject
		{
			var container:Sprite = new Sprite();
			var skinDisplObj:DisplayObject = _styles[skinStyle] as DisplayObject;
			var iconDispObj:DisplayObject = _styles[iconStyle] as DisplayObject;
			if (skinDisplObj != null)
			{
				container.addChild(skinDisplObj);
			}
			if (iconDispObj != null)
			{
				
				iconDispObj.x = _iconPosition.getPosX(width, iconDispObj.width);
				iconDispObj.y = _iconPosition.getPosY(height, iconDispObj.height);
				container.addChild(iconDispObj);
			}
			return container;
		}
		
		override public function get toggle():Boolean 
		{
			return super.toggle;
		}
		
		override public function set toggle(value:Boolean):void 
		{
			//super.toggle = value;
		}
		
		public function get groupName():String 
		{
			return _groupName;
		}
		
		public function set groupName(value:String):void 
		{
			_groupName = value;
		}
		override protected function setupSkinSizes():void 
		{
			//super.setupSkinSizes();
		}
		
		override protected function setupDefaulStyle():void 
		{
			var skinSprite:Sprite;
			skinSprite = new Sprite();
			drawCircle(skinSprite.graphics, 0xCCCCCC);
			setStyle(UP_ICON, skinSprite);
			
			skinSprite = new Sprite();
			drawCircle(skinSprite.graphics, 0x999999);
			setStyle(OVER_ICON, skinSprite);
			
			skinSprite = new Sprite();
			drawCircle(skinSprite.graphics, 0x666666);
			setStyle(DOWN_ICON, skinSprite);
			
			
			skinSprite = new Sprite();
			drawCircle(skinSprite.graphics, 0xCCCCCC, true);
			setStyle(SELECTED_UP_ICON, skinSprite);
			
			skinSprite = new Sprite();
			drawCircle(skinSprite.graphics, 0x999999, true);
			setStyle(SELECTED_OVER_ICON, skinSprite);
			
			skinSprite = new Sprite();
			drawCircle(skinSprite.graphics,  0x666666, true);
			setStyle(SELECTED_DOWN_ICON, skinSprite);
		}
		
		override protected function setupHitArea():void 
		{
			if (buttonUp != null)
			{
				return;
			}
			var hitObj:DisplayObject = null;
			hitObj = _styles[HIT_AREA] as DisplayObject;
			if (hitObj != null)
			{
				_button.hitTestState = hitObj;
			}
			
			var btnRect:Rectangle = _button.getBounds(this);
			var txtRect:Rectangle = new Rectangle();
			txtRect.x = textField.x;
			txtRect.y = textField.y;
			txtRect.width = textField.width;
			txtRect.height = textField.height;
			
			/*txtRect.x = testShape.x;
			txtRect.y = testShape.y;
			txtRect.width = testShape.width;
			txtRect.height = testShape.height;*/
			
			var rect:Rectangle = new Rectangle()
			rect.left = Math.min(btnRect.left, txtRect.left);
			rect.top = Math.min(btnRect.top, txtRect.top);
			rect.right = Math.max(btnRect.right, txtRect.right);
			rect.bottom = Math.max(btnRect.bottom, txtRect.bottom);
			
			var hitSprite:Sprite = new Sprite();
			var gr:Graphics = hitSprite.graphics;
			gr.beginFill(0x000000);
			gr.drawRect(rect.x, rect.y, rect.width, rect.height);
			gr.endFill();
			
			//draw test rect
			/*gr = this.graphics;
			gr.clear();
			gr.lineStyle(1, 0xFF0000);
			gr.drawRect(rect.x, rect.y, rect.width, rect.height);*/
			
			_button.hitTestState = hitSprite;
		}
		
		override protected function onInvalidate(e:Event):void 
		{
			//height = (_styles[UP_SKIN] as DisplayObject).height;
			super.onInvalidate(e);
			setupDelimiter();
		}
		
		
		private function setupDelimiter():void 
		{
			var delimiter:DisplayObject;
			if (_currentDelimiter != null && _currentDelimiter.parent != null)
			{
				_currentDelimiter.parent.removeChild(_currentDelimiter);
			}
			
			if (_selected)
			{
				delimiter = _styles[SELECTED_DELIMITER] as DisplayObject;
				if (delimiter != null)
				{
					var bounds:Rectangle = getBoundRectComponent();
					if (delimiter.parent != null)
					{
						delimiter.parent.removeChild(delimiter);
					}
					addChild(delimiter);
					delimiter.x = _delimiterPos.getPosX(width,delimiter.width);
					delimiter.y = _delimiterPos.getPosY(bounds.height, delimiter.height);
					_currentDelimiter = delimiter;
				}
			}else
			{
				
			}
		}
		
		override protected function setupButtonSkin():void 
		{
			if (buttonUp == null)
			{
				if (_selected)
				{
					_button.upState = fuseStyles(SELECTED_UP_SKIN, SELECTED_UP_ICON);
					_button.downState = fuseStyles(SELECTED_DOWN_SKIN, SELECTED_DOWN_ICON);
					_button.overState = fuseStyles(SELECTED_OVER_SKIN, SELECTED_OVER_ICON);
				}else
				{
					_button.upState = fuseStyles(UP_SKIN, UP_ICON);
					_button.downState = fuseStyles(DOWN_SKIN, DOWN_ICON);
					_button.overState = fuseStyles(OVER_SKIN, OVER_ICON);
				}
			}else
			{
				super.setupButtonSkin();
			}
		}
		
		
		
		private function drawCircle(gr:Graphics, color:uint, drawCentr:Boolean = false):void
		{
			gr.lineStyle(1, 0x000000);
			gr.beginFill(color, 1);
			gr.drawCircle(radius, radius, radius);
			gr.endFill();
			
			if (drawCentr)
			{
				var smallRad:Number = radius * 0.5;
				gr.lineStyle();
				gr.beginFill(0x000000, 1);
				gr.drawCircle(radius + radius * 0.5 - smallRad,radius + radius * 0.5 - smallRad, smallRad);
				gr.endFill();
			}
		}
		
		
		override public function set selected(value:Boolean):void 
		{
			if(value)
			{
				RadioButton.clear(this);
			}
			super.selected = value;
		}
		
		public function getBoundRectComponent(drawRect:Boolean = false, color:uint = 0):Rectangle
		{
			var btnRect:Rectangle = new Rectangle();
			btnRect.x = _button.x;
			btnRect.y = _button.y;
			btnRect.width = _button.width;
			btnRect.height = _button.height;
			
			var txtRect:Rectangle = new Rectangle();
			txtRect.x = textField.x;
			txtRect.y = textField.y;
			txtRect.width = textField.width;
			txtRect.height = textField.height;
			
			
			var minX:Number = Math.min(btnRect.left, txtRect.left);
			var minY:Number = Math.min(btnRect.top, txtRect.top);
			var maxX:Number = Math.max(btnRect.right, txtRect.right);
			var maxY:Number= Math.max(btnRect.bottom, txtRect.bottom);
			
			var rect:Rectangle = new Rectangle(minX, minY, maxX - minX, maxY - minY)
			if (drawRect)
			{
				log("minX : " +minX + " minY: " + minY + " maxX: " + maxX + " maxY: " + maxY);
				log("txtRect x: " + txtRect.x + " y: " + txtRect.y + 
					" w: " + txtRect.width + "h: " + txtRect.height);
				log("btnRect x: " + btnRect.x + " y: " + btnRect.y + " w: " 
					+ btnRect.width + " h: " + btnRect.height);
				var gr:Graphics = this.graphics;
				gr.clear();
				gr.lineStyle(1, color);
				gr.drawRect(rect.x, rect.y, rect.width, rect.height);
			}
			
			return rect;
			
		}
		
		public function get iconPosition():PositionInfo 
		{
			return _iconPosition;
		}
		
		public function get value():Object 
		{
			return _value;
		}
		
		public function set value(value:Object):void 
		{
			_value = value;
		}
		
		private function log(msg:String):void
		{
			Logger.log(this, msg);
		}
	}

}